package students_test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class sql extends JFrame implements ActionListener{
    JFrame mm = new JFrame("您好！请您先登录！");
    JTextField t2 = new JTextField(null,15);
    JTextField t4 = new JPasswordField(null,15);
    public String zh = null;
    JRadioButton b = new JRadioButton("管理员");
    JRadioButton b1 = new JRadioButton("教师");
    JRadioButton b2 = new JRadioButton("学生");
    public void jiemian(){
        mm.setSize(300,340);
        mm.setVisible(true);
        mm.setLocation(200,300);
        mm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗体关闭方式
        JLabel t1 = new JLabel("ID号:");
        JLabel t3 = new JLabel("密码:");
        JButton denglu2 = new JButton("登录");//添加登录按钮
        denglu2.setContentAreaFilled ( false );//设置登录按钮透明
        Container n = mm.getContentPane();//获取窗体容器
        n.setLayout(null);
        t1.setBounds(42,125,75,35);
        t2.setBounds(80,125,150,35);
        t3.setBounds(40,180,75,35);
        t4.setBounds(80,180,150,35);
        denglu2.setBounds( 102,245,70,30);
        n.add(t1);n.add(t2);n.add(t3);n.add(t4); n.add(denglu2);
        b.setBounds(130,85,70,30);
        b1.setBounds(70,85,70,30);
        b2.setBounds( 190,85,70,30);
        ButtonGroup rg =new ButtonGroup();
        b.setSelected( false);
        b1.setSelected(true);
        b2.setSelected(false);
        n.add(b1);n.add(b2);n.add(b);
        rg.add(b2);
        rg.add(b);
        n.add(b1);
        rg.add(b1);
        b.setContentAreaFilled( false);
        b1.setContentAreaFilled (false);
        b2.setContentAreaFilled( false );
        denglu2.addActionListener( this);//添加登录按钮事件
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        JButton denglu2=(JButton)e.getSource();//获取登录按钮
        if(b.isSelected()){
            zh = t2.getText();
            String mima = t4.getText();
            try{
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//加载驱动
                Connection c = DriverManager.getConnection("jdbc:odbc:学生信息系统");//连接数据库
                Statement s = c.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
                String sql = "select * from 教师信息表 where 教师ID = '"+zh+"' and 登录密码= '"+mima+"'";
                ResultSet r = s.executeQuery(sql);
                r.last();
                if((r.getRow()==1)){
                    mm.setVisible( false );
                    r.beforeFirst();
                    while(r.next()){
                        JOptionPane.showMessageDialog(
                            null,r.getString("教师姓名")
                            +" 老师!您好!欢迎登录教师管理系统!"
                        );
                    }
                    JFrame mmm = new JFrame ();
                    mmm.setSize(400,420);
                    mmm.setVisible (true);
                    mmm.setLocation(200,300);
                    mmm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    JLabel aaa = new JLabel("学生信息管理:");
                    JLabel aaa1 = new JLabel("学生成绩管理:");
                    JButton b1=new JButton("显示学生信息");
                    b1.setBounds(40,45,140,30);
                    JButton b2=new JButton("添加学生信息");
                    b2.setBounds(200,45,140,30);    
                    JButton b3=new JButton("修改学生信息");
                    b3.setBounds(40,90,140,30);
                    JButton b4=new JButton("删除学生信息");
                    b4.setBounds(200,90,140,30);
                    JButton b5=new JButton("录人学生成绩");
                    b5.setBounds(40,170,140,30);
                    JButton b6=new JButton("修改学生成绩");
                    b6.setBounds(200,170,140,30);
                    JButton b7=new JButton("删除学生成绩");
                    b7.setBounds(40,215,140,30);
                    JButton b8=new JButton("查询学生成绩");
                    b8.setBounds(200,215,140,30);
                    JButton b9=new JButton("所有成绩排名");
                    b9.setBounds(40,260,140,30);
                    JButton b12=new JButton("单科成绩排名");
                    b12.setBounds(200,260,140,30);
                    JButton b10=new JButton("退出系统");
                    b10.setBounds(60,325 ,100,30);
                    JButton b11=new JButton("修改密码");
                    b11.setBounds(220,325,100,30);
                    aaa.setBounds(15,15,90,30);
                    aaa1.setBounds(15,140,90,30);
                    Container n=mmm.getContentPane( );
                    n.setLayout(null);
                    n.add(b1);
                    n.add(b2);
                    n.add(b3);
                    n.add(b4);
                    n.add(b5);
                    n.add(b6);
                    n.add(b7);
                    n.add(b8);
                    n.add(b9);
                    n.add(b10);
                    n.add(aaa);
                    n.add(aaa1 );
                    n.add(b11);
                    n.add(b12);
                    b1.addActionListener(this);
                    b2.addActionListener(this);
                    b3.addActionListener(this);
                    b4.addActionListener(this);
                    b5.addActionListener(this);
                    b6.addActionListener(this);
                    b7.addActionListener(this);
                    b8.addActionListener(this);
                    b9.addActionListener(this);
                    b11.addActionListener(this);
                    b12.addActionListener(this);
                    b10.addActionListener(new ActionListener() {
                        public void actionPerformed(final ActionEvent e) {
                            System.exit(0);
                        }
                    });
                    ((JComponent)mmm.getContentPane()).setOpaque( false);
                    java.net.URL url = this.getClass().getResource("未命名.jpg");
                    ImageIcon img = new ImageIcon(url);
                    JLabel background = new JLabel(img);
                    mmm.getLayeredPane().add(background, Integer.MIN_VALUE);
                    background.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
                }else{
                    JOptionPane.showMessageDialog(null,"教师ID或密码有误!请重新输人!");
                }
            }catch(Exception e1){
                e1.printStackTrace( );
            }
        }      
    }
    public static void main(String args[]){
        sql app=new sql( );
        app.jiemian( );
    }
}